package com.example.administrator.wallet.view.fragment.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MsgBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.administrator.wallet.R.id.tv_content;
import static com.example.administrator.wallet.R.id.tv_date;
import static com.example.administrator.wallet.R.id.tv_date2;
import static com.example.administrator.wallet.R.id.tv_title;


public class InformFragment extends Fragment {

    private ListView mList;
    private MsgBean msgBean;
    private List<MsgBean.DataBean> datas;
    private SimpleAdapter adapter;
    private int code = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notice, null, false);

        mList = (ListView)view.findViewById(R.id.mList);
        datas=new ArrayList<>();
        final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
        reqequestManager.httpPost(Api.BaseUrl + Api.Message_List, Api.getMessageList(SharedPrefsUtil.getValue(getActivity(),"wallet", "token",""),"1"), new IRequestCallback(){
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                msgBean = gson.fromJson(Response, MsgBean.class);
                code = msgBean.getCode();
                if (code == 200) {
                    datas.addAll(msgBean.getData());
                    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
                    for (int i = 0; i < datas.size(); i++) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        MsgBean.DataBean dataBean = datas.get(i);
                        map.put("date", dataBean.getAdd_time().substring(5, 10));
                        map.put("mContent", dataBean.getContent());
                        map.put("title", dataBean.getTitle());
                        map.put("date1", dataBean.getAdd_time().substring(0, 10));
                        list.add(map);
                    }
                    adapter = new SimpleAdapter(getContext(), list, R.layout.item_msg, new String[]{"date", "title", "mContent", "date1"}, new int[]{tv_date, tv_title, tv_content, tv_date2});
                    mList.setAdapter(adapter);
                }else{
                    Toast.makeText(getActivity(),R.string.Nomessage,Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(String error) {
                Toast.makeText(getActivity(),"获取通知失败",Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

}